/*
 * Copyright (c) 2018. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.xlkj.receiver;

import android.annotation.SuppressLint;
import android.app.KeyguardManager;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.util.Log;

import com.xlkj.faces_input.MainActivity;

@SuppressWarnings("ALL")
public class PowerUtil {


    private static final String TAG="PowerUtil";

    public static void unlockDevice(Context context){
        // 屏幕解锁
        KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
        KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("unLock");
        keyguardLock.reenableKeyguard();
        keyguardLock.disableKeyguard(); // 解锁
    }
    public static void lockDevice(Context context){
        try{
            DevicePolicyManager dpm = (DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE);
            ComponentName who = new ComponentName(context, AdminReceiver.class);

            if(dpm.isAdminActive(who)){
                dpm.lockNow();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public static void restartApp(Context context){
        Log.i(TAG, "restartApp: APP启动");
        // 获取电源管理器对象
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        boolean screenOn = pm.isScreenOn();
        if (!screenOn) {
            // 获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag
            @SuppressLint("InvalidWakeLockTag") PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "bright");
            wl.acquire(10000); // 点亮屏幕
            wl.release(); // 释放
        }
        Intent in = new Intent(context, MainActivity.class);
        in.addCategory(Intent.CATEGORY_LAUNCHER);
        in.setAction(Intent.ACTION_MAIN);
        in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        context.startActivity(in);
//        for (int i=0;i<19;i++){
//            Intent in = new Intent(context, MainActivity.class);
//            in.addCategory(Intent.CATEGORY_LAUNCHER);
//            in.setAction(Intent.ACTION_MAIN);
//            in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
//            PendingIntent p = PendingIntent.getActivity(context,0,in,0);
//            try {
//                p.send();
//            } catch (PendingIntent.CanceledException e) {
//                e.printStackTrace();
//            }
//        }
    }

}
